package com.zydl.f9164m.fragment.sms;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.zydl.f9164m.R;
import com.zydl.f9164m.basis.BasisFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SMSAlarmFragment extends BasisFragment {
    private String[] arrAlarmSmsText;


    @BindView(R.id.alarmListView)
    ListView listView;
    MyListAdapter myListAdapter;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.fragment_smsalarm);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        myListAdapter = new MyListAdapter();
        listView.setAdapter(myListAdapter);
    }

    public void readData(String[] _arrAlarmSmsTexts) {
        arrAlarmSmsText = new String[5];

        for (int i=0;i<_arrAlarmSmsTexts.length;i++){
            if(i>=5){
                break;
            }
            arrAlarmSmsText[i] = _arrAlarmSmsTexts[i];
        }

        for (int i=0;i<arrAlarmSmsText.length;i++){
            if (TextUtils.isEmpty(arrAlarmSmsText[i])) {
                arrAlarmSmsText[i]="";
            }
        }

        if (isAdded()) {
            myListAdapter.notifyDataSetChanged();
        }

    }

    public String[] saveData() {
        List<String> resultDataList = new ArrayList<>();
        for (String data : arrAlarmSmsText) {
            if (!TextUtils.isEmpty(data)) {
                resultDataList.add(data);
            }
        }

        String[] resultData = new String[5];
        for (int i=0;i<resultDataList.size();i++){
            resultData[i]=resultDataList.get(i);
        }

        for (int i=0;i<resultData.length;i++){
            if (TextUtils.isEmpty(resultData[i])) {
                resultData[i]="";
            }
        }

        return resultData;
    }

    private class MyListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (arrAlarmSmsText != null && arrAlarmSmsText.length != 0) {
                return arrAlarmSmsText.length;
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return arrAlarmSmsText[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //ViewHolder holder = null;
            final ViewHolder holder;
            if (convertView == null) {

                holder = new ViewHolder();
                LayoutInflater inflater = getActivity().getLayoutInflater();
                convertView = inflater.inflate(R.layout.smsalarm_listview_item, null);
                holder.textView1 = (TextView) convertView.findViewById(R.id.smsNum);
                holder.editText1 = (EditText) convertView.findViewById(R.id.smsAlarmNum);
                holder.editText1.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();
            }

            holder.ref = position;

            holder.textView1.setText(position + 1 + "");
            holder.editText1.setText(arrAlarmSmsText[position]);
            holder.editText1.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    arrAlarmSmsText[holder.ref] = arg0.toString().trim();
                }
            });

            return convertView;
        }

        private class ViewHolder {
            TextView textView1;
            EditText editText1;
            int ref;
        }
    }
}
